package com.sgb.kjwl.viewmodel.manage;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.view.entity.MenuEntity;
import com.sgb.kjwl.model.entity.manage.TheEnterpriseOfEntity;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.fragment.home.EnterpriseManageChildFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EnterpriseManageChildFragModel extends BaseViewModel {
    private EnterpriseManageChildFragment fragment;
    private HomeRepertory repertory;
    private Map map;
    private int pageNum;
    private int pageSize = 5;
    private int total;
    private String areaCode;
    List<MenuEntity> list;

    public EnterpriseManageChildFragModel(EnterpriseManageChildFragment fragment, HomeRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    //卖家推荐需求
    public void getRecommendedList(String longitude, String latitude) {
        if (map == null)
            map = new HashMap();
        map.put("longitude", longitude);
        map.put("latitude", latitude);
//        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getRecommendedList(map).enqueue(new Callback<BaseEntity<List<TheEnterpriseOfEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<TheEnterpriseOfEntity>>> call, Response<BaseEntity<List<TheEnterpriseOfEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null)
                        return;
                    fragment.showOptionView(response.body().getData());
                } else {
                    MToast.showToast(fragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<TheEnterpriseOfEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(fragment.getActivity(), t.getMessage());
            }
        });
    }

    //最新入驻
    public void getNewestComList(String longitude, String latitude) {
        if (map == null)
            map = new HashMap();
        map.put("longitude", longitude);
        map.put("latitude", latitude);
//        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getNewestComList(map).enqueue(new Callback<BaseEntity<List<TheEnterpriseOfEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<TheEnterpriseOfEntity>>> call, Response<BaseEntity<List<TheEnterpriseOfEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() == null)
                        return;
                    fragment.showOptionView(response.body().getData());
                } else {
                    MToast.showToast(fragment.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<TheEnterpriseOfEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(fragment.getActivity(), t.getMessage());
            }
        });
    }

    public void saveProvinceModel(String name, String code) {
        repertory.setAreaCode(code);
        repertory.setAreaName(name);
    }
}
